package com.whu.waterworks.impl;

import java.util.List;

import org.springframework.orm.hibernate4.HibernateTemplate;

import com.whu.waterworks.dao.RealtimedataDAO;
import com.whu.waterworks.model.Realtimedata;
import com.whu.waterworks.model.RealtimedataId;
/**
 * 
 * @author wangying
 *
 */
public class RealtimedataDAOImpl implements RealtimedataDAO{
	private HibernateTemplate hibernateTemplate;
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@Override
	public boolean  save(Realtimedata entity) {
		// TODO Auto-generated method stub
		try {
			
			hibernateTemplate.save(entity);
			//System.out.println("realDataSave");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
		
	}

	@Override
	public boolean delete(Realtimedata entity) {
		// TODO Auto-generated method stub
		try {
			hibernateTemplate.delete(entity);
			
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
		return true;
	}

	@Override
	public boolean update(Realtimedata entity) {
		// TODO Auto-generated method stub
		try {
			hibernateTemplate.update(entity);
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.toString());
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public Realtimedata findById(RealtimedataId id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Realtimedata> findByProperty(String propertyName, Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Realtimedata> findByValue(Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Realtimedata> findAll() {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<Realtimedata> realtimedatas  = (List<Realtimedata>)hibernateTemplate.find("from Realtimedata rd");
		if(realtimedatas.size()==0)
			return null;
		return realtimedatas;
	}

	
	@Override
	public List<Realtimedata> findByIdTodayAll(String id,String type) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<Realtimedata> realtimedatas = (List<Realtimedata>)hibernateTemplate.find("from Realtimedata rd where rd.id.stationnum='"+id+"' and rd.id.type like '%"+type+"%' order by rd.id.time desc");
		if(realtimedatas.size()==0)
			return null;
		System.out.println("****RealtimedataDAOImpl realtimedatas="+realtimedatas.size());
		return realtimedatas;
	}

	

	@Override
	public List<Realtimedata> findByIdAllInTime(String id, String type,
			String endTime, String beginTime) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<Realtimedata> realtimedatas = (List<Realtimedata>)hibernateTemplate.find("from Realtimedata rd where rd.id.stationnum='"+id+"' and rd.id.type like '%"+type
				+"%' and rd.id.time>="+beginTime+" and rd.id.time<"+endTime +
				" order by rd.id.time desc");
		if(realtimedatas.size()==0)
			return null;
		return realtimedatas;
	}

	@Override
	public List<Realtimedata> findAllById(String id) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<Realtimedata> realtimedatas  = (List<Realtimedata>)hibernateTemplate.find("from Realtimedata rd where rd.id.stationnum='"+id+"'  order by rd.id.time desc");
		if(realtimedatas.size()==0)
			return null;
		return realtimedatas;
	}

}
